﻿// -----------------------------------------------------------------------
// <copyright file="TwoImgOperation.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace IPPackage.ImageAlgebra
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;

    /// <summary>
    /// two image operation
    /// </summary>
    public class TwoImgOperation : ImageOperation.Operation
    {
        /// <summary>
        /// subtract two image.
        /// </summary>
        /// <param name="img2">second image.</param>
        /// <returns>new image</returns>
        public Bitmap Sub(Color[,] img2)
        {
            ImageOperation.Resize newImg2 = new IPPackage.ImageOperation.Resize();
            newImg2.BufferOfImage = img2;
            Bitmap newImg = newImg2.Resize_bilinear(this.BufferOfImage.GetLength(0), this.BufferOfImage.GetLength(1));
            img2 = new Color[BufferOfImage.GetLength(0), BufferOfImage.GetLength(1)];
            img2 = newImg2.NewBufferOfImage;
            newImg = new Bitmap(BufferOfImage.GetLength(0), BufferOfImage.GetLength(1));
             this.NewBufferOfImage = new Color[BufferOfImage.GetLength(0), BufferOfImage.GetLength(1)];
            try
            {
                for (int i = 0; i < BufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < BufferOfImage.GetLength(0); j++)
                    {
                        this.NewBufferOfImage[j, i] = Color.FromArgb(Math.Abs(BufferOfImage[j, i].R - img2[j, i].R), Math.Abs(BufferOfImage[j, i].G - img2[j, i].G), Math.Abs(BufferOfImage[j, i].B - img2[j, i].B));
                        newImg.SetPixel(j, i, this.NewBufferOfImage[j, i]);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                newImg = null;
            }

            return newImg;
        }

        /// <summary>
        /// Adds two image.
        /// </summary>
        /// <param name="img2">image two.</param>
        /// <param name="fraction">The fraction.</param>
        /// <returns>modified image</returns>
        public Bitmap Add(Color[,] img2, float fraction)
        {
            ImageOperation.Resize newImg2 = new IPPackage.ImageOperation.Resize();
            newImg2.BufferOfImage = img2;
            Bitmap newImg = newImg2.Resize_bilinear(this.BufferOfImage.GetLength(0), this.BufferOfImage.GetLength(1));
            img2 = new Color[BufferOfImage.GetLength(0), BufferOfImage.GetLength(1)];
            img2 = newImg2.NewBufferOfImage;
            newImg = new Bitmap(BufferOfImage.GetLength(0), BufferOfImage.GetLength(1));
            Color[,] newBuffer = new Color[BufferOfImage.GetLength(0), BufferOfImage.GetLength(1)];
            try
            {
                for (int i = 0; i < BufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < BufferOfImage.GetLength(0); j++)
                    {
                        newBuffer[j, i] = Color.FromArgb((int)Math.Abs((int)(BufferOfImage[j, i].R * fraction) + (img2[j, i].R * (1 - fraction))), (int)Math.Abs((BufferOfImage[j, i].G * fraction) + (img2[j, i].G * (1 - fraction))), (int)Math.Abs((BufferOfImage[j, i].B * fraction) + (img2[j, i].B * (1 - fraction))));
                        newImg.SetPixel(j, i, newBuffer[j, i]);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                newImg = null;
            }

            return newImg;
        }
    }
}
